package com.christian.thread.deadlock;

import java.util.concurrent.TimeUnit;

/**
 * 文件描述：多线程死锁问题
 *
 * @Author：wang
 * @Date：2020-01-25 09:09:52
 */
public class DeadLockCase01 {

    /**
     * 多线程锁对象
     */
    private Boolean lock1 = true;
    /**
     * 多线程锁对象
     */
    private Boolean lock2 = false;


    public static void main(String[] args) {
        new DeadLockCase01().dealLock();
    }

    /**
     * 死锁方法
     */
    public void dealLock() {
        Thread thread1 = new Thread(() -> {
            synchronized (lock1) {
                try {
                    TimeUnit.SECONDS.sleep(2);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                synchronized (lock2) {
                    System.out.println("1");
                }
            }
        }, "Thread1");

        Thread thread2 = new Thread(() -> {
            synchronized (lock2) {
                synchronized (lock1) {
                    System.out.println("2");
                }
            }
        }, "Thread2");

        thread1.start();
        thread2.start();
    }
}
